package com.itcncp.wechat.app.rpc.service.impl;
import com.itcncp.common.annotation.BaseService;
import com.itcncp.common.base.BaseServiceImpl;
import com.itcncp.common.sms.Sms;
import com.itcncp.common.util.DateUtils;
import com.itcncp.common.util.MD5Util;
import com.itcncp.wechat.app.dao.mapper.UserVerifyJnlMapper;
import com.itcncp.wechat.app.dao.model.UserVerifyJnl;
import com.itcncp.wechat.app.dao.model.UserVerifyJnlExample;
import com.itcncp.wechat.app.rpc.api.UserVerifyJnlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Random;

/**
* UserVerifyJnlService实现
* Created by itcncp on 2018/1/12.
*/
@Service
@Transactional
@BaseService
public class UserVerifyJnlServiceImpl extends BaseServiceImpl<UserVerifyJnlMapper, UserVerifyJnl, UserVerifyJnlExample>
        implements UserVerifyJnlService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserVerifyJnlServiceImpl.class);

    @Autowired
    UserVerifyJnlMapper userVerifyJnlDao;


    /**
     * 验证注册短信码
     * @param mobile
     * @param verifyCode
     * @param type  验证码类型 1.用户注册验证码 2.登录确认验证码 3.修改密码验证码 4.身份验证验证码 5.信息变更验证码 6.活动确认验证码
     * @return
     */
    @Override
    public  UserVerifyJnl verifyRegisterCode(String mobile, String verifyCode,Integer type) {

        // 验证短信码
        UserVerifyJnl verify = userVerifyJnlDao.selectByPrimaryKey( mobile );

        if (verify == null) {
            return null;
        }

        if (verify.getState() == 2) {
            return null;
        }

        Long verifyTime = verify.getFairTime().getTime();
        Long currentTime = System.currentTimeMillis();
        if ((verifyTime - currentTime) < 0) {
            return null;
        }

        verifyCode = MD5Util.signature( verifyCode );
        if(!verify.getVerifCode().equals( verifyCode )){
            return  null;
        }

        //短信验证类型是否相同
        if(verify.getType().intValue() != type){
            return null;
        }

        //状态 1:未验证 2:已验证
        verify.setState(new Byte( "2" ));
        userVerifyJnlDao.updateByPrimaryKeySelective( verify );

        return verify;
    }

    /**
     * 发送注册短信码
     * @param mobile
     * @return
     */
    @Override
    public UserVerifyJnl getRegisterCode(String mobile) {
        UserVerifyJnl verify =  userVerifyJnlDao.selectByPrimaryKey( mobile );

        //判断是否新增
        Boolean isAdd = false;

        if(verify == null) {
            verify = new UserVerifyJnl();
            isAdd = true;
        }

        //设置5分钟后失效
        verify.setCreateTime(new Date());
        Date endTime =  DateUtils.getAfterMinute(5);
        verify.setFairTime(endTime);
        //状态   1:未验证 2:已验证
        verify.setState( new Byte( "1" ) );
        //验证码类型 1.用户注册验证码 2.登录确认验证码 3.修改密码验证码 4.身份验证验证码 5.信息变更验证码 6.活动确认验证码
        verify.setType( new Byte( "1" ) );
        Random random = new Random();
        int xPassword = random.nextInt(8999);
        String smsCode = String.valueOf(xPassword);

        String password = MD5Util.signature(smsCode);
        verify.setVerifCode( password );
        verify.setMobile(mobile);

        if(isAdd) {
            userVerifyJnlDao.insert( verify );
        } else {
            userVerifyJnlDao.updateByPrimaryKeySelective(verify);
        }

        //短信注册验证码
        //String format = SendFormat.verifyCodeFormat(smsCode);
        //SendSMS.send(mobile, format);
        Sms.sendRegister(mobile, smsCode);

        return verify;
    }


    /**
     * 发送密码修改验证码
     * @param mobile
     * @return
     */
    @Override
    public UserVerifyJnl getPasswordCode(String mobile) {
        UserVerifyJnl verify =  userVerifyJnlDao.selectByPrimaryKey( mobile );

        //判断是否新增
        Boolean isAdd = false;

        if(verify == null) {
            verify = new UserVerifyJnl();
            isAdd = true;
        }

        //设置5分钟后失效
        verify.setCreateTime(new Date());
        Date endTime =  DateUtils.getAfterMinute(5);
        verify.setFairTime(endTime);
        //状态   1:未验证 2:已验证
        verify.setState( new Byte( "1" ) );
        //验证码类型 1.用户注册验证码 2.登录确认验证码 3.修改密码验证码 4.身份验证验证码 5.信息变更验证码 6.活动确认验证码
        verify.setType( new Byte( "3" ) );
        Random random = new Random();
        int xPassword = random.nextInt(8999);
        String smsCode = String.valueOf(xPassword);

        String password = MD5Util.signature(smsCode);
        verify.setVerifCode( password );
        verify.setMobile(mobile);

        if(isAdd) {
            userVerifyJnlDao.insert( verify );
        } else {
            userVerifyJnlDao.updateByPrimaryKeySelective(verify);
        }

        Sms.sendPassword(mobile, smsCode);

        return verify;
    }

    /**
     * 发送信息修改验证码
     * @param mobile
     * @return
     */
    @Override
    public UserVerifyJnl getInfoUpdateCode(String mobile) {
        UserVerifyJnl verify =  userVerifyJnlDao.selectByPrimaryKey( mobile );

        //判断是否新增
        Boolean isAdd = false;

        if(verify == null) {
            verify = new UserVerifyJnl();
            isAdd = true;
        }

        //设置5分钟后失效
        verify.setCreateTime(new Date());
        Date endTime =  DateUtils.getAfterMinute(5);
        verify.setFairTime(endTime);
        //状态   1:未验证 2:已验证
        verify.setState( new Byte( "1" ) );
        //验证码类型 1.用户注册验证码 2.登录确认验证码 3.修改密码验证码 4.身份验证验证码 5.信息变更验证码 6.活动确认验证码
        verify.setType( new Byte( "5" ) );
        Random random = new Random();
        int xPassword = random.nextInt(8999);
        String smsCode = String.valueOf(xPassword);

        String password = MD5Util.signature(smsCode);
        verify.setVerifCode( password );
        verify.setMobile(mobile);

        if(isAdd) {
            userVerifyJnlDao.insert( verify );
        } else {
            userVerifyJnlDao.updateByPrimaryKeySelective(verify);
        }

        Sms.sendInfoUpdate(mobile,smsCode);

        return verify;
    }

}